﻿using DevExpress.XtraEditors.Controls;
using RecruitStudents.Utility;
using RecruitStudents.Utility.Helper;
using RecruitStudents.Utility.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RecruitStudents.Client.Base
{
    public partial class Base_Dic_NMTSignUp_Option : BaseForm
    {
        BLL.ServiceBase service = new BLL.ServiceBase();
        TB_Dic_NMT_SignUp model = null;
        private string strDataID = string.Empty;
        public Base_Dic_NMTSignUp_Option(string _DataID)
        {
            InitializeComponent();
            strDataID = _DataID;
            BindCity();
            if (strDataID.Length > 0)
            {
                BindModel();
            }
        }
        #region 加载考点省份        
        void BindCity()
        {
            this.cmbKD.Properties.NullText = "";
            this.cmbKD.Properties.Columns.Add(new LookUpColumnInfo("DicNO", "省份代码"));
            this.cmbKD.Properties.Columns.Add(new LookUpColumnInfo("DicName", "省份名称"));
            this.cmbKD.Properties.DataSource = new BLL.ServiceComm().GetDicDataTableByDicTableName("TB_Dic_ExamProvince");
            this.cmbKD.Properties.DisplayMember = "DicName";
            this.cmbKD.Properties.ValueMember = "DicNO";
            this.cmbKD.Properties.Columns[0].Visible = false;
        }
        #endregion
        private void BindModel()
        {
            model = service.GetDicNMTSignUpModel(strDataID);
            if (model != null)
            {
                cmbKD.EditValue = model.NativePlaceNO;
                cmbKD.Enabled = false;
                txtIndex_Begin.Text = model.Index_Begin.ToSafeString();
                txtIndex_End.Text = model.Index_End.ToSafeString();
                txtMask.Text = model.Mask;
                txtNMT_SignUp_Len.Text = model.NMT_SignUp_Len.ToSafeString();
                model.Attach();

            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (cmbKD.EditValue.ToSafeString().Length == 0)
            {
                DevHelper.ShowErrorMessageBox("请先选择考点省份！");
                cmbKD.Focus();
                return;
            }
            if (strDataID.IsNullOrEmptyString())
            {
                model = new TB_Dic_NMT_SignUp();
                if (service.GetDicNMTSignUpModel(cmbKD.EditValue.ToSafeString()) != null)
                {
                    DevHelper.ShowErrorMessageBox("考点省份已经存在,请重新选择！");
                    cmbKD.Focus();
                    return;
                }
                else
                {
                    model.NativePlaceName = cmbKD.SelectedText;
                    model.NativePlaceNO = cmbKD.EditValue.ToSafeString();
                }
            }
            else
            {
                model.Attach();
            }
            model.Index_Begin = txtIndex_Begin.Text.ToSafeInt();
            model.Index_End = txtIndex_End.Text.ToSafeInt();
            model.Mask = txtMask.Text;
            model.NMT_SignUp_Len = txtNMT_SignUp_Len.Text.ToSafeInt();
            if (service.SaveDicNMTSignUp(model))
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }
    }
}
